package com.ruoyi.web.controller.teaching;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.teaching.domain.TeachingCourse;
import com.ruoyi.teaching.domain.TeachingCourseFolder;
import com.ruoyi.teaching.service.ITeachingCourseFolderService;
import com.ruoyi.teaching.service.ITeachingCourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.TeachingCourseWare;
import com.ruoyi.teaching.service.ITeachingCourseWareService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;

/**
 * 课件管理Controller
 *
 * @author William
 * @date 2022-10-12
 */
@Controller
@RequestMapping("/teaching/ware")
public class TeachingCourseWareController extends BaseController {
    private String prefix = "teaching/ware";
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private ITeachingCourseWareService teachingCourseWareService;
    @Autowired
    private ITeachingCourseService courseService;
    @Autowired
    private ITeachingCourseFolderService folderService;

//    @RequiresPermissions("teaching:ware:view")
    @GetMapping()
    public String ware() {
        return prefix + "/ware";
    }

    /**
     * 查询课件管理列表
     */
//    @RequiresPermissions("teaching:ware:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingCourseWare teachingCourseWare) {
        startPage();
        List<TeachingCourseWare> list = teachingCourseWareService.selectTeachingCourseWareList(teachingCourseWare);
        return getDataTable(list);
    }

    /**
     * 导出课件管理列表
     */
//    @RequiresPermissions("teaching:ware:export")
    @Log(title = "课件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingCourseWare teachingCourseWare) {
        List<TeachingCourseWare> list = teachingCourseWareService.selectTeachingCourseWareList(teachingCourseWare);
        ExcelUtil<TeachingCourseWare> util = new ExcelUtil<TeachingCourseWare>(TeachingCourseWare.class);
        return util.exportExcel(list, "课件管理数据");
    }

    /**
     * 新增课件管理
     */
    @GetMapping("/add")
    public String add(@RequestParam("courseId") Long courseId, @RequestParam(value = "folderId", required = false) Long folderId, ModelMap mmap) {
        TeachingCourseFolder folder = new TeachingCourseFolder();
        if (StringUtils.isNotNull(folderId)) {
            folder = folderService.selectTeachingCourseFolderByFolderId(folderId);
        }
        mmap.put("folder", folder);
        TeachingCourse course = courseService.selectTeachingCourseByCourseId(courseId);
        if (StringUtils.isNotNull(course)) {
            mmap.put("course", course);
        }

        return prefix + "/add";
    }

    /**
     * 新增保存课件管理
     */
//    @RequiresPermissions("teaching:ware:add")
    @Log(title = "课件管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("thumbFile") MultipartFile thumb, @RequestParam("file") MultipartFile file, TeachingCourseWare teachingCourseWare) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        if (!thumb.isEmpty()) {
            // 上传缩略图
            String fileName = FileUploadUtils.upload(filePath, thumb);
            String url = serverConfig.getUrl() + fileName;
            teachingCourseWare.setThumb(url);
        }
        if (!file.isEmpty()) {
            //上传课件
            teachingCourseWare.setFileId(0L);
            String fileName = FileUploadUtils.upload(filePath, file);
            teachingCourseWare.setFilePath(fileName);
            teachingCourseWare.setUploadTime(new Date());
        }
        teachingCourseWare.setCreateId(getUserId());
        teachingCourseWare.setCreateBy(getLoginName());
        teachingCourseWare.setCreateTime(new Date());
        return toAjax(teachingCourseWareService.insertTeachingCourseWare(teachingCourseWare));
    }

    /**
     * 修改课件管理
     */
//    @RequiresPermissions("teaching:ware:edit")
    @GetMapping("/edit/{wareId}")
    public String edit(@PathVariable("wareId") Long wareId, ModelMap mmap) {
        TeachingCourseWare teachingCourseWare = teachingCourseWareService.selectTeachingCourseWareByWareId(wareId);
        mmap.put("teachingCourseWare", teachingCourseWare);
        return prefix + "/edit";
    }

    /**
     * 修改保存课件管理
     */
//    @RequiresPermissions("teaching:ware:edit")
    @Log(title = "课件管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam(value = "thumbFile", required = false) MultipartFile thumb, @RequestParam(value = "file", required = false) MultipartFile file, TeachingCourseWare teachingCourseWare) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        if (StringUtils.isNotNull(thumb) && !thumb.isEmpty()) {
            // 上传缩略图
            String fileName = FileUploadUtils.upload(filePath, thumb);
            String url = serverConfig.getUrl() + fileName;
            teachingCourseWare.setThumb(url);
        }
        if (StringUtils.isNotNull(file) && !file.isEmpty()) {
            //上传课件
            teachingCourseWare.setFileId(0L);
            String fileName = FileUploadUtils.upload(filePath, file);
            teachingCourseWare.setFilePath(fileName);
            teachingCourseWare.setUploadTime(new Date());
        }
        teachingCourseWare.setUpdateBy(getLoginName());
        teachingCourseWare.setUpdateTime(new Date());
        return toAjax(teachingCourseWareService.updateTeachingCourseWare(teachingCourseWare));
    }

    /**
     * 删除课件管理
     */
//    @RequiresPermissions("teaching:ware:remove")
    @Log(title = "课件管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(teachingCourseWareService.deleteTeachingCourseWareByWareIds(ids));
    }

    /**
     * 用户状态修改
     */
    @Log(title = "课件管理", businessType = BusinessType.UPDATE)
//    @RequiresPermissions("teaching:ware:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(TeachingCourseWare ware) {
        return toAjax(teachingCourseWareService.updateTeachingCourseWare(ware));
    }
}
